package com.taotao.context.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.comm.pojo.EasyUITreeNode;
import com.taotao.context.service.ContentCategoryService;
import com.taotao.dao.TbContentCategoryMapper;
import com.taotao.pojo.TbContentCategory;
import com.taotao.pojo.TbContentCategoryExample;
import com.taotao.pojo.TbContentCategoryExample.Criteria;
import com.taotao.pojo.TbItemExample;
import com.taotao.utils.TaotaoResult;
@Service 
public class ContentCategoryServiceImpl implements ContentCategoryService{
	@Autowired
	private TbContentCategoryMapper contentCategoryMapper;

	@Override
	public List<EasyUITreeNode> getContentCategoryNode(long pid) {
		
		TbContentCategoryExample example=new TbContentCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(pid);
		criteria.andStatusEqualTo(1);
		
		List<TbContentCategory> contentCategoryList = contentCategoryMapper.selectByExample(example);
		
		List<EasyUITreeNode> list = new ArrayList<>();
		for (TbContentCategory contentCategory : contentCategoryList) {
			EasyUITreeNode node = new EasyUITreeNode();
			node.setId(contentCategory.getId());
			node.setText(contentCategory.getName());
			node.setState(contentCategory.getIsParent()?"closed":"open");
			list.add(node);
		}
		return list;
	}

	@Override
	public TaotaoResult insertCategory(long pid, String name) {
		TbContentCategory contentCategory = new TbContentCategory();
		contentCategory.setCreated(new Date());
		contentCategory.setName(name);
		contentCategory.setParentId(pid);
		contentCategory.setSortOrder(1);
		contentCategory.setIsParent(false);
		//状态。可选值:1(正常),2(删除)
		contentCategory.setStatus(1);
		contentCategory.setUpdated(new Date());
		long id = contentCategoryMapper.insertSelective(contentCategory);
		
		contentCategory.setId(id);
		
		TbContentCategory parentCatgory = contentCategoryMapper.selectByPrimaryKey(pid);
		System.out.println("parentCatgory.getIsParent():"+parentCatgory.getIsParent());
		if(!parentCatgory.getIsParent()){
			parentCatgory.setIsParent(true);
			contentCategoryMapper.updateByPrimaryKey(parentCatgory);
		}
		return TaotaoResult.ok(contentCategory);
	}

	@Override
	public TaotaoResult deleteCategory(long id) {
		long pid = deleteContentCategoryAll(id);
		//判断该id的父节点的开闭?
		TbContentCategoryExample example = new TbContentCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(pid);
		//得到有该pid的节点
		List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
		//判断  如果不存在子集  就将父集的isParent设为false
		if(list.size()==0){
			TbContentCategory contentCategory = new TbContentCategory();
			contentCategory.setId(pid);
			contentCategory.setIsParent(false);
			contentCategoryMapper.updateByPrimaryKeySelective(contentCategory);
		}
		
		return TaotaoResult.ok();
	}
	
	public long deleteContentCategoryAll(long id){
		TbContentCategory contentCategory = contentCategoryMapper.selectByPrimaryKey(id);
		if(contentCategory.getIsParent()){
			TbContentCategoryExample example = new TbContentCategoryExample();
			Criteria criteria = example.createCriteria();
			criteria.andParentIdEqualTo(id);
			//设置条件status 不等于2的   1,正常  2,删除
			criteria.andStatusNotEqualTo(2);
			List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
			for (TbContentCategory tbContentCategory : list) {
				//递归调用
				deleteContentCategoryAll(tbContentCategory.getId());
			}
		}
		contentCategory.setStatus(2);
		contentCategoryMapper.updateByPrimaryKey(contentCategory);
		return contentCategory.getParentId();
	}

	@Override
	public TaotaoResult updateCategory(long id, String name) {
		TbContentCategory contentCategory = new TbContentCategory();
		contentCategory.setId(id);
		contentCategory.setName(name);
		contentCategory.setUpdated(new Date());
		return TaotaoResult.ok(contentCategory);
	}

}
